function [savings_EWM] = generate_ewm_results_table(covariate,wave,winter_prevuse,SMC,fbase,scaled_ATT,s_now_batch,s_now_batch_delta_CI,rule,tcost)
% This function summarizes EWM point estimates and confidence intervals
% into a summary table 

% Inputs:
% (1) covariate: indicates covariate for analysis: income, size, vintage,
% minimum of baseline consumption, maximum of baseline consumption, or
% standard deviation of consumption
% (2) wave: indicates whether the output is wave-specific (=3,6,or 7) or
% for the pooled sample (=0) 
% (3) winter_prevuse: indicates whether baseline consumption is calculated 
% as the mean of consumption in winter months (Jan and Feb) or as the mean
% of specified pre-treatment periods
% (4) SMC: =1 use social marginal cost; =0 use retail electricity price
% (5) fbase: string used to indicate the propensity score and baseline
% months specification for output table 
% (6) scaled_ATT: scaled att as a benchmark 
% (7) s_now_batch: timestamp used to identify EWM bootstraps 
% (8) s_now_batch_delta_CI: timestamp used to identify bootstraps for the
% difference between EWM savings and scaled ATT 
% (9) rule: treatment rule 
% (10) tcost: private marginal cost of implementing the program per household
%  >0 represents cost savings; =0 represents kWh reduction 

% Outputs:
% (1) savings_EWM: summary table of savings 
%% obtain point estimates 
switch winter_prevuse 
    case 1
        s_winter = '_winter';
    case 0
        s_winter = '';
end
if (tcost)>0 % cost_savings
    if (SMC)
        s_cost = 'SMC';
    else
        s_cost = 'PMC';
    end
elseif (tcost)==0
    s_cost='kwh';
end

if rule=="quadrant"
    filename_coefs=sprintf('coef_quadrant_%s_%s%s_wave%1.0f.mat',covariate,s_cost,s_winter,wave);
elseif rule=="cubic"
    filename_coefs=sprintf('coef_cubic_%s_%s%s_wave%1.0f.mat',covariate,s_cost,s_winter,wave);
elseif rule=="onedim"
    filename_coefs=sprintf('coef_onedim_%s_baseline_%s%s_wave%1.0f.mat',covariate,s_cost,s_winter,wave);
end
    file = sprintf('%s_%s',fbase,filename_coefs);
S=load(file);

%extract content of S_quadrant
c_fieldnames = fieldnames(S);
for ifield = 1:length(c_fieldnames)
    field_ = c_fieldnames{ifield};
    eval(sprintf('%s=S.%s;',field_,field_))
end

% obtain scaled att
scaled_att_PE=scaled_ATT;
%% obtain ewm point estimate (PE) and confidence interval (CI)
% generate suffix for bootstrap folder 
if (tcost)
    if (SMC)
        s_speci = 'smc';
    else
        s_speci = 'pmc';
    end
else
    s_speci='kwh';
end
if rule=="quadrant"
    fpath_BS = ['.\intermediate_data\EWM_results\BS\quadrant\',...
     s_now_batch,'_',covariate,'_',s_speci,'\'];
elseif rule=="cubic"
    fpath_BS = ['.\intermediate_data\EWM_results\BS\cubic\',...
     s_now_batch,'_',covariate,'_',s_speci,'\'];
elseif rule=="onedim"
    fpath_BS = ['.\intermediate_data\EWM_results\BS\quadrant\',...
        s_now_batch,'_','univariate','_',s_speci,'\'];
end
if ~exist(fpath_BS,'dir')
   mkdir(fpath_BS); 
end

[bs_result] = pool_BS(s_now_batch,rule,fpath_BS);

if rule=="quadrant" || rule=="onedim"
    vhats_p = bs_result.maxWb;
    vhats_n = bs_result.minWb;
    vhats_abs = max([vhats_p -vhats_n],[],2);
elseif rule=="cubic"
    v_pd_itr=bs_result.v_pd;
    v_pi_itr=bs_result.v_pi;
    v_nd_itr=bs_result.v_nd;
    v_ni_itr=bs_result.v_ni;
    
    vhats_p= -min(v_pd_itr,v_pi_itr);
    vhats_n= -min(v_nd_itr,v_ni_itr);
    vhats_abs = max([vhats_p vhats_n],[],2);
end

%% calculate PE of V(G_hat) for savings and CI for savings 
if rule=="quadrant" || rule=="onedim"
    percent = sum(nw.*in_Ghat)/n;
    savings = sum(gu.*in_Ghat)/n;
    ci_lb = (minW-prctile(vhats_abs,95))/n;
    ci_ub = (minW+prctile(vhats_abs,95))/n;
elseif rule=="cubic"
    percent = mean(in_Ghat);
    savings = nanmean(g.*in_Ghat)*Yscale;
    ci_lb = (v-prctile(vhats_abs,95))*Yscale/n;
    ci_ub = (v+prctile(vhats_abs,95))*Yscale/n;
end

%% hypothesis testing on the difference in ewm savings and scaled att to
% get the CI using inference approach two

delta_ewm_rct = savings - scaled_att_PE; %PE of difference on the original sample

% get bootstrap V_Ghat and scaled_att
if rule=="quadrant" % load the delta_CI bootstraps (ran separately from the EWM loop)
    fpath_BS = ['.\intermediate_data\EWM_results\BS\quadrant\',...
     s_now_batch_delta_CI,'_',covariate,'_',s_speci,'_','deltaCI','\'];
elseif rule=="cubic"
    fpath_BS = ['.\intermediate_data\EWM_results\BS\cubic\',...
     s_now_batch_delta_CI,'_',covariate,'_',s_speci,'_','deltaCI','\'];
elseif rule=="onedim"
    fpath_BS = ['.\intermediate_data\EWM_results\BS\quadrant\',...
        s_now_batch_delta_CI,'_','univariate','_',s_speci,'_','deltaCI','\'];
end
if ~exist(fpath_BS,'dir')
   mkdir(fpath_BS); 
end

[bs_result_delta_CI] = pool_BS_delta_CI(s_now_batch_delta_CI,rule,fpath_BS);

V_Ghat = bs_result_delta_CI.W_bs_Ghat; 
scaled_att = bs_result_delta_CI.scaled_ATT_bs;

se_delta=sqrt(var(V_Ghat-scaled_att));
ci_delta_lb = delta_ewm_rct - 1.96*se_delta;
ci_delta_ub = delta_ewm_rct + 1.96*se_delta;

%% Output for table 
savings_EWM_rules=nan(1,4);
savings_EWM_rules=array2table(savings_EWM_rules,'VariableNames',{'Covariates','Share\ treated','Savings\ from\ EWM\ rules','Difference\ in\ savings\ between\ EWM\ RCT'}); 
format short g
savings_EWM_rules.(1) = {covariate};
savings_EWM_rules.(2) =round(percent*100);
savings_EWM_rules.(3) =round(savings,2);
savings_EWM_rules.(4) =round(delta_ewm_rct,2);

ci_lb=round(ci_lb,2);
ci_ub=round(ci_ub,2);

ci_delta_lb=round(ci_delta_lb,2);
ci_delta_ub=round(ci_delta_ub,2);

ci_table=nan(1,4);
ci_table=array2table(ci_table,'VariableNames',{'Covariates','Share\ treated','Savings\ from\ EWM\ rules','Difference\ in\ savings\ between\ EWM\ RCT'}); 
cilb_string=string(ci_lb);
ciub_string=string(ci_ub);
ci_for_table=strcat('(',cilb_string,',',ciub_string,')');
ci_delta_lb_string=string(ci_delta_lb);
ci_delta_ub_string=string(ci_delta_ub);
ci_delta_for_table=strcat('(',ci_delta_lb_string,',',ci_delta_ub_string,')');

ci_table.(1)="";
ci_table.(2)="";
ci_table.(3)=ci_for_table;
ci_table.(4)=ci_delta_for_table;
savings_EWM=[savings_EWM_rules;ci_table];
end

